package com.hqd.ch03.v26.aop.support;

import com.hqd.ch03.v26.aopalliance.aop.Advice;
import com.hqd.ch03.v26.aware.BeanFactoryAware;
import com.hqd.ch03.v26.factory.BeanFactory;

public abstract class AbstractBeanFactoryPointcutAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware {

    private String adviceBeanName;

    private BeanFactory beanFactory;

    private transient volatile Advice advice;


    @Override
    public Advice getAdvice() {
        Advice advice = this.advice;
        if (advice != null) {
            return advice;
        }

        if (this.beanFactory.isSingleton(this.adviceBeanName)) {
            advice = this.beanFactory.getBean(this.adviceBeanName, Advice.class);
            this.advice = advice;
            return advice;
        } else {
            advice = this.advice;
            if (advice == null) {
                advice = this.beanFactory.getBean(this.adviceBeanName, Advice.class);
                this.advice = advice;
                return advice;
            }
        }
        return this.advice;
    }

    public void setAdvice(Advice advice) {
        this.advice = advice;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    public String getAdviceBeanName() {
        return this.adviceBeanName;
    }

    public void setAdviceBeanName(String adviceBeanName) {
        this.adviceBeanName = adviceBeanName;
    }
}
